<template>
  <div class="app-container">
    <!-- 后台管理-权限 -->
    <div class="top">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input placeholder="根据用户名搜索" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="clear">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
        <el-form>
          <el-form-item>
            <el-button type="success" icon="el-icon-edit" @click="dialogOpen">新增权限组</el-button>
          </el-form-item>
        </el-form>
      </el-form>
      <el-alert :title="`共${total}条记录`" type="info" :closable="false" show-icon></el-alert>
      <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
      <!-- el-table-column 的 type="selection" 属性已经为表格添加了默认的选择框。
      但如果你想要自定义选择框的样式或行为，你可以在 el-table-column 的模板插槽中进行操作。 -->
      <el-table-column type="selection" width="180"></el-table-column>
      <el-table-column prop="title" label="用户名" width="180"></el-table-column>
      <el-table-column label="日期">
        <template #default="{row}">
          <!-- | parseTime() -->
          {{getTime(row.create_date)}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(row.id)"></el-button>
        </template>
      </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="100"
        layout="prev, pager, next, sizes, jumper"
        :total="total">
      </el-pagination>
      <!-- 弹窗 -->
      <permissions-dialog ref="dialog" :dialog-visible="visible" @close-dalog="dialogClose" @get-list="getList"></permissions-dialog>
    </div>
  </div>
</template>

<script>
import PermissionsDialog from '@/views/base/permissions/components/permissions-dialog'
import {permissionsList, delPermission} from '@/api/permissions'
import {parseTime} from '@/utils/index.js'
export default {
  name: 'Permissions',
  components: {
    PermissionsDialog
  },
  data() {
    return {
      form: {
        page: 1,
        pagesize: 10,
        title: ''
      },
      tableData:[],
      checked: false,
      total: 0,
      visible: false,
      multipleSelection: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 解析时间
    getTime(time){
      return parseTime(time)
    },
    handleSelectionChange(val) {
        this.multipleSelection = val;
        // console.log(val);
      },
    // 获取表格
    async getList() {
      const res = await permissionsList(this.form)
      // console.log(res);
      this.total = res.counts
      this.tableData = res.list
    },
    // 每页条数改变
    handleSizeChange(size) {
      this.form.pagesize= size
      this.getList()
    },
    // 当前页改变
    handleCurrentChange(page) {
      this.form.page = page
      this.getList()
    },
    // 搜索
    search () {
      this.form.page = 1
      this.getList()
    },
    // 清除
    clear() {
      this.form = {
        page: 1,
        pagesize: 10,
        title: ''
      }
      this.getList()
    },
    dialogOpen() {
      this.visible = true
    },
    dialogClose() {
      this.visible = false
    },
    // 编辑
    edit(id) {
      this.dialogOpen()
      // 父组件调子组件上的方法
      this.$refs.dialog.getDetail(id)
    },
    // 删除
    del(id) {
      // 询问用户
      this.$confirm('此操作将永久删除该用户件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async()=> {
          // 确认删除
          // 调删除接口
          await delPermission(id)
          // 提示用户
          this.$message.success('成功删除了用户！')
          // 刷新列表
          this.getList()
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #fff;
  margin: 10px;
}
</style>